const { tenantsModel } = require('./models/tenantsModel');
//获取所有已审核租客
module.exports.getTenants = async ({ pageSize, currentPage }) => {
    //获取总条数
    const total = await tenantsModel.countDocuments({ state: 2 });
    //获取总页数
    const pages = Math.ceil(total / pageSize)
    const tenants = await tenantsModel.find({ state: 2 }).limit(pageSize - 0).skip((currentPage - 1) * pageSize);
    return {
        tenants, total, pages,
    }
},
    //获取所有未审核租客
    module.exports.getAuditTenants = async ({ pageSize, currentPage }) => {
        //获取总条数
        const total = await tenantsModel.countDocuments({ state: 1 });
        //获取总页数
        const pages = Math.ceil(total / pageSize)
        const auditTenants = await tenantsModel.find({ state: 1 }).limit(pageSize - 0).skip((currentPage - 1) * pageSize);
        return {
            auditTenants, total, pages,
        }
    },
    //获取要修改的未审核租客
    module.exports.updateAuditTenants = async ({ _id }) => {
        const data = await tenantsModel.updateOne({ _id }, { state: 2 });
        return data;
    }
//查询租客
module.exports.searchTenants = async (params) => {
    //获取总条数
    const total = (await tenantsModel.find({
        [params.searchType]: { $regex: params.searchValue, $options: '$i' }
    })).length;
    //计算总页数
    const pages = Math.ceil(total / params.pageSize);
    //模糊查询
    const tenants = await tenantsModel.find({
        [params.searchType]: { $regex: params.searchValue, $options: '$i' }
    }).limit(params.pageSize - 0).skip((params.currentPage - 1) * params.pageSize);
    return {
        total, pages, tenants
    };
}


//通过id获取租客
module.exports.getTenantsById = async (id) => {
    const data = await tenantsModel.find({ _id: id.id });
    return data;
}


//通过id修改租客的信息
module.exports.changeTenantsById = async (code) => {
    const data = await tenantsModel.updateOne({ _id: code.id }, { name: code.name, phone: code.phone });
    return data;
}